/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package br.com.delogic.simuladorbancario.view;

import br.com.delogic.simuladorbancario.business.AgenciaBusiness;
import br.com.delogic.simuladorbancario.model.Agencia;
import javax.swing.JOptionPane;

/**
 *
 * @author julioVanni
 */
public class AgenciaView extends javax.swing.JInternalFrame {

    AgenciaBusiness agenciaBusiness = AgenciaBusiness.getInstance();
    
    /**
     * Creates new form AgenciaView
     */
    public AgenciaView() {
        initComponents();
    }

    void salvar(){
        Agencia agencia = new Agencia();
        agencia.setCodigo(Integer.parseInt(txtCodigoAgencia.getText()));
        agencia.setNome(txtNomeAgencia.getText());
        agencia.setCnpj(txtCnpjAgencia.getText());
        agencia.setEndereco(txtEnderecoAgencia.getText());
        agencia.setAtiva(chkAgenciaAtiva.isSelected());
        
        agenciaBusiness.cadastrarAgencia(agencia);
        
        JOptionPane.showMessageDialog(this, "Agência Cadastrada com sucesso!");
        
        limpar();
    }
    
    void limpar(){
        
        txtCodigoAgencia.setText(null);
        txtNomeAgencia.setText(null);
        txtCnpjAgencia.setText(null);
        txtEnderecoAgencia.setText(null);
        chkAgenciaAtiva.setSelected(false);
        
        
    }
    
    void mostrarAgencia(Agencia agencia){
        
        txtNomeAgencia.setText(agencia.getNome());
        txtCnpjAgencia.setText(agencia.getCnpj());
        chkAgenciaAtiva.setSelected(agencia.isAtiva());
        txtEnderecoAgencia.setText(agencia.getEndereco());
 
    }
    
    
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        lblCodigoAgencia = new javax.swing.JLabel();
        txtCodigoAgencia = new javax.swing.JTextField();
        btnConsultarAgencia = new javax.swing.JButton();
        jSeparator1 = new javax.swing.JSeparator();
        lblNomeAgencia = new javax.swing.JLabel();
        lblCnpjAgencia = new javax.swing.JLabel();
        lblEnderecoAgencia = new javax.swing.JLabel();
        txtNomeAgencia = new javax.swing.JTextField();
        txtCnpjAgencia = new javax.swing.JTextField();
        txtEnderecoAgencia = new javax.swing.JTextField();
        chkAgenciaAtiva = new javax.swing.JCheckBox();
        jSeparator2 = new javax.swing.JSeparator();
        btnSalvarAgencia = new javax.swing.JButton();
        btnDesativarAgencia = new javax.swing.JButton();
        btnReativarAgencia = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        lblCodigoAgencia.setText("Código Agência:");

        txtCodigoAgencia.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtCodigoAgenciaActionPerformed(evt);
            }
        });

        btnConsultarAgencia.setText("Consultar");
        btnConsultarAgencia.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnConsultarAgenciaActionPerformed(evt);
            }
        });

        lblNomeAgencia.setText("Nome:");

        lblCnpjAgencia.setText("CNPJ:");

        lblEnderecoAgencia.setText("Endereço:");

        txtNomeAgencia.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtNomeAgenciaActionPerformed(evt);
            }
        });

        txtCnpjAgencia.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtCnpjAgenciaActionPerformed(evt);
            }
        });

        chkAgenciaAtiva.setText("Agência Ativa");
        chkAgenciaAtiva.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                chkAgenciaAtivaActionPerformed(evt);
            }
        });

        btnSalvarAgencia.setText("Salvar");
        btnSalvarAgencia.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSalvarAgenciaActionPerformed(evt);
            }
        });

        btnDesativarAgencia.setText("Desativar");
        btnDesativarAgencia.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnDesativarAgenciaActionPerformed(evt);
            }
        });

        btnReativarAgencia.setText("Reativar");
        btnReativarAgencia.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnReativarAgenciaActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(layout.createSequentialGroup()
                                        .addGap(18, 18, 18)
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                            .addComponent(lblCnpjAgencia, javax.swing.GroupLayout.Alignment.TRAILING)
                                            .addComponent(lblNomeAgencia, javax.swing.GroupLayout.Alignment.TRAILING)))
                                    .addComponent(lblEnderecoAgencia))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(txtCnpjAgencia, javax.swing.GroupLayout.PREFERRED_SIZE, 172, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                        .addComponent(chkAgenciaAtiva))
                                    .addComponent(txtNomeAgencia)
                                    .addComponent(txtEnderecoAgencia, javax.swing.GroupLayout.DEFAULT_SIZE, 297, Short.MAX_VALUE)))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(btnSalvarAgencia)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(btnDesativarAgencia)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(btnReativarAgencia))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(lblCodigoAgencia)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(txtCodigoAgencia, javax.swing.GroupLayout.PREFERRED_SIZE, 106, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(btnConsultarAgencia)))
                        .addGap(40, 40, 40))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jSeparator2)
                            .addComponent(jSeparator1))
                        .addGap(29, 29, 29))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(19, 19, 19)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblCodigoAgencia)
                    .addComponent(txtCodigoAgencia, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnConsultarAgencia))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtNomeAgencia, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lblNomeAgencia))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtCnpjAgencia, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(chkAgenciaAtiva)
                    .addComponent(lblCnpjAgencia))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(lblEnderecoAgencia)
                    .addComponent(txtEnderecoAgencia, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jSeparator2, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnSalvarAgencia)
                    .addComponent(btnDesativarAgencia)
                    .addComponent(btnReativarAgencia))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void txtCodigoAgenciaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtCodigoAgenciaActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_txtCodigoAgenciaActionPerformed

    private void chkAgenciaAtivaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_chkAgenciaAtivaActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_chkAgenciaAtivaActionPerformed

    private void btnSalvarAgenciaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSalvarAgenciaActionPerformed
        // TODO add your handling code here:
        salvar();
    }//GEN-LAST:event_btnSalvarAgenciaActionPerformed

    private void txtNomeAgenciaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtNomeAgenciaActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_txtNomeAgenciaActionPerformed

    private void txtCnpjAgenciaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtCnpjAgenciaActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_txtCnpjAgenciaActionPerformed

    private void btnConsultarAgenciaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnConsultarAgenciaActionPerformed
        
        int codigoAgencia = Integer.parseInt(txtCodigoAgencia.getText());
        
        Agencia agencia = agenciaBusiness.consultarAgencia(codigoAgencia);
        
        mostrarAgencia(agencia);        

    }//GEN-LAST:event_btnConsultarAgenciaActionPerformed

    private void btnDesativarAgenciaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDesativarAgenciaActionPerformed
        // TODO add your handling code here:
        int codigoAgencia = Integer.parseInt(txtCodigoAgencia.getText());
        boolean desativado = agenciaBusiness.desativarAgencia(codigoAgencia);
        if (desativado == true){
            JOptionPane.showMessageDialog(this, "Agência desativa com sucesso!");
        } else if (desativado == false){
             JOptionPane.showMessageDialog(this, "Agência não foi desativa!");
        }
             
    }//GEN-LAST:event_btnDesativarAgenciaActionPerformed

    private void btnReativarAgenciaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnReativarAgenciaActionPerformed
        // TODO add your handling code here:
        int codigoAgencia = Integer.parseInt(txtCodigoAgencia.getText());
        boolean ativado = agenciaBusiness.ativarAgencia(codigoAgencia);
        if (ativado == true){
            JOptionPane.showMessageDialog(this, "Agência ativada com sucesso!");
        } else if (ativado == false){
             JOptionPane.showMessageDialog(this, "Agência não foi ativada!");
        }
    }//GEN-LAST:event_btnReativarAgenciaActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(AgenciaView.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(AgenciaView.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(AgenciaView.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(AgenciaView.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new AgenciaView().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnConsultarAgencia;
    private javax.swing.JButton btnDesativarAgencia;
    private javax.swing.JButton btnReativarAgencia;
    private javax.swing.JButton btnSalvarAgencia;
    private javax.swing.JCheckBox chkAgenciaAtiva;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JSeparator jSeparator2;
    private javax.swing.JLabel lblCnpjAgencia;
    private javax.swing.JLabel lblCodigoAgencia;
    private javax.swing.JLabel lblEnderecoAgencia;
    private javax.swing.JLabel lblNomeAgencia;
    private javax.swing.JTextField txtCnpjAgencia;
    private javax.swing.JTextField txtCodigoAgencia;
    private javax.swing.JTextField txtEnderecoAgencia;
    private javax.swing.JTextField txtNomeAgencia;
    // End of variables declaration//GEN-END:variables
}
